
package com.wyzz.hr.bo.impl;

import java.util.List;
import org.apache.axis.utils.StringUtils;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IPerfschematypeBO;
import com.wyzz.hr.dao.IPerfschematypeDAO;
import com.wyzz.hr.pojo.Perfschematype;


public class PerfschematypeBOImpl implements IPerfschematypeBO {

	public PerfschematypeBOImpl() {
	}

	public List loadAll() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Perfschematype.class);
		dc.addOrder(Order.asc(Perfschematype.PROP_PST_SORT_ID));
		return perfschematypeDAO.findByCriteria(dc);
	}

	public Perfschematype save(Perfschematype pst) {
		if (StringUtils.isEmpty(pst.getPstId())) {
			if (getPstByName(pst.getPstName()) != null) {
				pst.setInfo("EXISTED");
				return pst;
			}
			pst.setPstSortId(getMaxSortId());
			perfschematypeDAO.saveObject(pst);
		} else {
			perfschematypeDAO.updateObject(pst);
		}
		pst.setInfo("SUCC");
		return pst;
	}

	public void delete(String pstId) {
		perfschematypeDAO.deleteObject(com.wyzz.hr.pojo.Perfschematype.class,
				pstId);
	}

	public int getRelatePsCount(String pstId) {
		String hql = (new StringBuilder()).append(
				"select count(*) from Perfschema where psPstId.pstId='")
				.append(pstId).append("'").toString();
		List list = perfschematypeDAO.exeHqlList(hql);
		if (list != null && list.size() > 0)
			return ((Long) list.get(0)).intValue();
		else
			return 0;
	}

	public Perfschematype getPstByName(String pstName) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Perfschematype.class);
		dc.add(Restrictions.eq(Perfschematype.PROP_PST_NAME, pstName.trim()));
		List pstList = perfschematypeDAO.findByCriteria(dc);
		if (pstList != null && pstList.size() > 0)
			return (Perfschematype) pstList.get(0);
		else
			return null;
	}

	public void savePstOrder(String ids[]) {
		if (ids == null || ids.length == 0)
			return;
		int sort = 1;
		int len = ids.length;
		for (int i = 0; i < len; i++) {
			perfschematypeDAO.exeHql((new StringBuilder()).append(
					"update Perfschematype set pstSortId=").append(sort)
					.append(" where pstId ='").append(ids[i]).append("'")
					.toString());
			sort++;
		}

	}

	public synchronized Integer getMaxSortId() {
		String hql = "select max(pstSortId) as maxSortId from Perfschematype pst";
		List maxNoList = perfschematypeDAO.exeHqlList(hql);
		Integer maxID;
		if (maxNoList == null || maxNoList.size() < 1
				|| maxNoList.get(0) == null)
			maxID = Integer.valueOf(1);
		else
			maxID = Integer.valueOf(1 + ((Integer) (Integer) maxNoList.get(0))
					.intValue());
		return maxID;
	}

	public IPerfschematypeDAO getPerfschematypeDAO() {
		return perfschematypeDAO;
	}

	public void setPerfschematypeDAO(IPerfschematypeDAO perfschematypeDAO) {
		this.perfschematypeDAO = perfschematypeDAO;
	}

	private IPerfschematypeDAO perfschematypeDAO;
}
